'use strict';
import {connect} from 'dva/index';
import {Button, Form, Table} from 'antd';
import FormBase from '../../components/Base/FormBase';
import styles from "../index.less";
import ViewTitle from "../../components/ViewTitle";
import {ModalView} from "antd-extra";
import ModalCreateOrEditPersonnel from "../personnel/modal/ModalCreateOrEditPersonnel";
import React from "react";

const _ = require('lodash');

const table = 'experimentStore';

@Form.create()
@connect(({
            user,
            global,
            experiment
          }) => ({...user, ...global, ...experiment}))
export default class Store extends FormBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    this.props.dispatch({
      type: 'global/getModel',
      payload: {table, tenantCode: this.getTenantCode()}
    });
    this.props.dispatch({
      type: 'experiment/findStoreMany',
      payload: {table, tenantCode: this.getTenantCode()}
    });
  }

  onCreateOrEdit = record => {
    const model = this.props[`${table}Model`];
    const type = record ? 'experiment/updateStore' : 'experiment/createStore';
    const title=record ? '编辑' : '创建表单';
    ModalView.open(ModalCreateOrEditPersonnel, {title, width: '1080px'}, {
      model,record,
      callback: payload => {
        payload.code = record ? record.code : 'EXS' + Date.now();
        this.props.dispatch({type, payload}).then(res => {
          this.requestReturn(res);
          this.componentDidMount();
        });
      }
    });
  };
  onDelete = record => {
    const{code}=record
    this.props.dispatch({type:'experiment/deleteStore', payload:{code}}).then(res => {
      this.requestReturn(res);
      this.componentDidMount();
    });
  };

  render() {
    const {experimentStore} = this.props;
    const model = this.props[`${table}Model`];
    return <Form className={styles.content}>
      <ViewTitle title={'实验库存列表'}
                 actions={<Button
                   type="primary"
                   onClick={() =>this.onCreateOrEdit()}>添加</Button>}
      ></ViewTitle>
      <Table
        className={styles.antTable}
        bordered={true}
        columns={this.getDicTableCols(model, this.onCreateOrEdit, this.onDelete)}
        dataSource={experimentStore}
        scroll={{x: true}}
        pagination={{
          current: this.state.current,
          total: this.state.total,
          showSizeChanger: true,
          onChange: page => {
            this.state.current = page;
            this.componentDidMount();
          },
          onShowSizeChange: (current, pageSize) => {
            this.state.current = 1;
            this.state.pageSize = pageSize;
            this.componentDidMount();
          }
        }}
        // pagination={{pageSize: 100}}
        //scroll={{ x: 1300 }}
      ></Table>

    </Form>;
  }
}
